package com.feng.cloud.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.feng.cloud.common.annotation.ControllerEndpoint;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.response.RestResponse;
import com.feng.cloud.common.response.TableResultResponse;
import com.feng.cloud.system.entity.OAuthClientDetails;
import com.feng.cloud.system.service.OAuthClientDetailsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @ClassName OAuthClientDetailsController
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/30 10:31
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("client")
@RequiredArgsConstructor
public class OAuthClientDetailsController {

    private final OAuthClientDetailsService clientDetailsService;


    @GetMapping("secret/{clientId}")
    public RestResponse<String> getOriginClientSecret(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OAuthClientDetails oAuthClientDetails = clientDetailsService.selectByClientId(clientId);
        if (oAuthClientDetails == null) {
            return new RestResponse<>(SystemStatus.CLIENT_NOT_EXIST);
        }
        return new RestResponse<String>().data(oAuthClientDetails.getOriginSecret());
    }

    /**
     * 根据clientId 查询客户端
     * @param clientId clientId
     * @return RestResponse<OAuthClientDetails>
     */
    @GetMapping("{clientId}")
    public RestResponse<OAuthClientDetails> selectByClientId(@NotBlank(message = "{required}") @PathVariable String clientId) {
        OAuthClientDetails oAuthClientDetails = clientDetailsService.selectByClientId(clientId);
        if (oAuthClientDetails == null) {
            return new RestResponse<>(SystemStatus.CLIENT_NOT_EXIST);
        }
        return new RestResponse<OAuthClientDetails>().data(oAuthClientDetails);
    }

    @DeleteMapping("{clientIds}")
    @PreAuthorize("hasAuthority('client:delete')")
    @ControllerEndpoint(operation = "删除客户端", exceptionMessage = "删除客户端失败")
    public RestResponse<Void> deleteClients(@NotBlank(message = "{required}") @PathVariable String clientIds) {
        clientDetailsService.deleteClients(clientIds.split(StringPool.COMMA));
        return new RestResponse<>();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('client:update')")
    @ControllerEndpoint(operation = "修改客户端", exceptionMessage = "修改客户端失败")
    public RestResponse<OAuthClientDetails> updateClient(@Valid OAuthClientDetails clientDetails) {
        clientDetailsService.updateClient(clientDetails);
        return new RestResponse<OAuthClientDetails>().data(clientDetails);
    }
    /**
     * 添加客户端
     * @param clientDetails clientDetails
     * @return RestResponse<OAuthClientDetails>
     */
    @PostMapping
    @PreAuthorize("hasAuthority('client:add')")
    @ControllerEndpoint(operation = "新增客户端", exceptionMessage = "新增客户端失败")
    public RestResponse<OAuthClientDetails> addClient(@Valid OAuthClientDetails clientDetails) {
        clientDetailsService.addClient(clientDetails);
        return new RestResponse<OAuthClientDetails>().data(clientDetails);
    }

    /**
     * 分页查询
     * @param params params
     * @return TableResultResponse<OAuthClientDetails>
     */
    @GetMapping("page")
    @PreAuthorize("hasAuthority('client:view')")
    public TableResultResponse<OAuthClientDetails> selectByPage(@RequestParam Map<String,Object> params) {
        QueryRequest request = new QueryRequest(params);
        IPage<OAuthClientDetails> iPage = clientDetailsService.selectListByPage(request);
        return new TableResultResponse<>(iPage.getTotal(), iPage.getRecords());
    }
}
